package com.reefe.controller;

import com.reefe.entity.KindEditorResult;
import com.reefe.entity.Result;
import com.reefe.utils.FastDFSClient;
import com.reefe.utils.FastJsonConvert;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 图片上传Controller
 * Created by cynicism on 17/1/26.
 */
@Controller
public class PictureController {

    private static Logger logger = Logger.getLogger(PictureController.class);

    @Value("${fastdfs.base.url}")
    private String FASTDFS_BASE_URL;
    @Value("${fastdfs.conf.path}")
    private String FASTDFS_CONF_PATH;


    @RequestMapping("/image/imageUpload")
    @ResponseBody
    public String picUpload(@RequestParam("file") MultipartFile uploadFile, MultipartFile wangEditorH5File) {
        Result<String> result = new Result();
        FastDFSClient fastDFSClient = null;
        try {
            fastDFSClient = new FastDFSClient(FASTDFS_CONF_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (uploadFile != null) {

            String oName = uploadFile.getOriginalFilename();

            String extName = oName.substring(oName.lastIndexOf(".") + 1);
            try {
                String uploadUrl = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
                result.setSuccess(true);
                result.setMessage("上传成功");
                result.setResult(FASTDFS_BASE_URL + uploadUrl);
            } catch (Exception e) {
                logger.error("图片上传失败！");
                result.setSuccess(false);
                result.setMessage("上传失败");
            }
            return FastJsonConvert.convertObjectToJSON(result);
        } else if (wangEditorH5File != null) {
            String oName = wangEditorH5File.getOriginalFilename();

            String extName = oName.substring(oName.lastIndexOf(".") + 1);
            try {
                String uploadUrl = fastDFSClient.uploadFile(wangEditorH5File.getBytes(), extName);
                String url = FASTDFS_BASE_URL + uploadUrl;
                result.setSuccess(true);
                result.setMessage("上传成功");
                result.setResult(url);
            } catch (Exception e) {
                logger.error("图片上传失败！");
                result.setSuccess(false);
                result.setMessage("上传失败");
                e.printStackTrace();
            }
            return FastJsonConvert.convertObjectToJSON(result);
        }
        return "";
    }

    @RequestMapping(value = "/kindeditor/imageUpload",method = RequestMethod.POST)
    @ResponseBody
    public KindEditorResult kindeditor(@RequestParam("imgFile") MultipartFile uploadFile, HttpServletRequest request){
        KindEditorResult kindEditorResult=new KindEditorResult();
        FastDFSClient fastDFSClient = null;
        try {
            fastDFSClient = new FastDFSClient(FASTDFS_CONF_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (uploadFile != null) {
            String oName = uploadFile.getOriginalFilename();

            String extName = oName.substring(oName.lastIndexOf(".") + 1);

            try {
                String uploadUrl = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
                kindEditorResult.setError(0);
                kindEditorResult.setMessage("上传成功");
                kindEditorResult.setUrl(FASTDFS_BASE_URL + uploadUrl);
            } catch (Exception e) {
                logger.error("图片上传失败！");
                kindEditorResult.setError(1);
                kindEditorResult.setMessage("上传失败");
            }
        }
        return kindEditorResult;
    }
}
